﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TalksLog.Domain.Account.Entities;
using TalksLog.Domain.Account.Repositores;
using TalksLog.Domain.Core;

namespace TalksLog.Infrastructure.EFRepository
{
    class EmailVerficationRepository : IEmailVerificationRepository
    {
        private EFDbContext _context;

        public EmailVerficationRepository(RepositoryDataContextBase context)
        {
            _context = (context as RepositoryEFDataContext).DBContext;
        }

        public void Add(EmailVerification entity)
        {
            _context.Entry<EmailVerification>(entity).State = System.Data.Entity.EntityState.Added;
        }

        public EmailVerification Find(string id)
        {
            return _context.EmailVerifications.Find(id);
        }

        public EmailVerification FindLatestOneByEmail(string email)
        {
            var one = _context.EmailVerifications.Where(e => e.Email == email)
                .OrderByDescending(e => e.CreateTime)
                .FirstOrDefault();
            return one;
        }

        public void Remove(EmailVerification entity)
        {
            throw new NotImplementedException();
        }

        public void Update(EmailVerification entity)
        {
            _context.Entry<EmailVerification>(entity).State = System.Data.Entity.EntityState.Modified;
        }
    }
}
